<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #modal {
            height: 200px;
            width: 200px;
            line-height: 200px;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border: 1px solid black;
            text-align: center;
        }
    </style>
</head>
<body>
    <button id="open">打开弹框</button>
    <button id="close">关闭弹框</button>
    <script>
        // const Modul = function() {
        //     this.modalBox = document.createElement('div')
        //     this.modalBox.id = 'modal';
        //     this.modalBox.style.display = 'none';
        //     this.modalBox.innerHTML = '我是一个模态框';
        //     document.body.appendChild(this.modalBox)
        // }
        // const modal = new Modul();
        const Modal = (function(){
            let modal = null;
            return function() {
                if(!modal){
                    modal = document.createElement('div');
                    modal.id = 'modal';
                    modal.style.display = 'none';
                    modal.innerHTML = '我是一个模态框';
                    document.body.appendChild(modal)
                }
                return modal
            }
        })()
        const openButton = document.getElementById('open'),
                closeButton = document.getElementById('close');
        openButton.addEventListener('click', function() {
            // 显示弹框
            const modal = new Modal();
            modal.style.display = 'block'
        })
        closeButton.addEventListener('click', function() {
            // 隐藏弹框
            const modal = new Modal()
            modal.style.display = 'none'
        })
    </script>
</body>
</html>